/*
 * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Sun designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Sun in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 */

package com.sun.lwuit;

import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.events.DataChangedListener;
import com.sun.lwuit.plaf.Style;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.layouts.FlowLayout;
import com.sun.lwuit.plaf.UIManager;
import java.util.Date;
import java.util.Hashtable;

/**
 * Date widget for selecting a date/time value. 
 * <p>To localize strings for month names
 * use the values "Calendar.Month" in the resource localization e.g. "Calendar.Jan", "Calendar.Feb" etc...
 *
 * @author Iddo Ari
 */
public class Calendar extends Container {
    private Label month;
    private Label year;
    private MonthView mv;
    private static String[] MONTHS = new String[]{"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};

    /**
     * Creates a new instance of Calendar set to the given date based on time
     * since epoch (the java.util.Date convention)
     * 
     * @param time time since epoch
     */
    public Calendar(long time) {
        super(new BorderLayout());
        Container upper = new Container(new FlowLayout(Component.CENTER));
        mv = new MonthView(time, this);
        
        month = new Label(MONTHS[mv.getMonth()]);
        java.util.Calendar cal = java.util.Calendar.getInstance();
        cal.setTime(new java.util.Date(time));
        month.getStyle().setBgTransparency(0);
        int y = cal.get(java.util.Calendar.YEAR);
        year = new Label("" + y);
        year.getStyle().setBgTransparency(0);
        
        Container cnt = new Container(new BoxLayout(BoxLayout.X_AXIS));
        cnt.addComponent(month);
        cnt.addComponent(year);
        upper.addComponent(cnt);
        
        addComponent(BorderLayout.NORTH, upper);
        addComponent(BorderLayout.CENTER, mv);
    }
    
    /**
     * Constructs a calendar with the current date and time
     */
    public Calendar() {
        this(System.currentTimeMillis());
    }

    /**
     * @inheritDoc
     */
    protected String getUIID() {
        return "Calendar";
    }

    /**
     * Returns the time for the current calendar.
     * 
     * @return the time for the current calendar.
     */
    public long getSelectedDay() {
        return mv.getSelectedDay();
    }

    void componentChanged() {
        java.util.Calendar cal = java.util.Calendar.getInstance();

        cal.set(java.util.Calendar.YEAR, mv.getYear());
        cal.set(java.util.Calendar.MONTH, mv.getMonth());
        cal.set(java.util.Calendar.DAY_OF_MONTH, mv.getDayOfMonth());
        Hashtable t =  UIManager.getInstance().getResourceBundle();
        String text = MONTHS[mv.getMonth()];
        if(t != null) {
            Object o = t.get("Calendar." + text);
            if(o != null) {
                text = (String)o;
            }
        } 
        month.setText(text);
        year.setText("" + mv.getYear());
        month.getParent().revalidate();
    }

    /**
     * Return the date object matching the current selection
     * 
     * @return the date object matching the current selection
     */
    public Date getDate() {
        return new Date(mv.getSelectedDay());
    }

    /**
     * Sets the current date in the view
     * 
     * @param d new date
     */
    public void setDate(Date d) {
        mv.setSelectedDay(d.getTime());
    }
    
    /**
     * @inheritDoc
     */
    public void paint(Graphics g) {
        super.paint(g);
    }

    /**
     * @inheritDoc
     */
    public void refreshTheme() {
        mv.refreshTheme();
    }
    
    /**
     * @inheritDoc
     * @deprecated 
     */
    public void setStyle(Style s) {
        setUnSelectedStyle(s);
    }

    /**
     * @inheritDoc
     */
    public void setUnSelectedStyle(Style s) {
        super.setUnSelectedStyle(s);
        // make sure the labels have the style of the calendar and not the style of a label
        month.setUnSelectedStyle(s);
        year.setUnSelectedStyle(s);
    }

    /**
     * @inheritDoc
     */
    public void setSelectedStyle(Style s) {
        super.setSelectedStyle(s);
        // make sure the labels have the style of the calendar and not the style of a label
        month.setSelectedStyle(s);
        year.setSelectedStyle(s);
    }

    /**
     * Sets the style of the month view component within the calendar
     * 
     * @param s style for the month view 
     * @deprecated 
     */
    public void setMonthViewStyle(Style s) {
        mv.setStyle(s);
    }

    /**
     * Sets the style of the month view component within the calendar
     * 
     * @return the style of the month view
     * @deprecated 
     */
    public Style getMonthViewStyle() {
        return mv.getStyle();
    }

    /**
     * Sets the selected style of the month view component within the calendar
     * 
     * @param s style for the month view 
     */
    public void setMonthViewSelectedStyle(Style s) {
        mv.setSelectedStyle(s);
    }

    /**
     * Sets the un selected style of the month view component within the calendar
     * 
     * @param s style for the month view 
     */
    public void setMonthViewUnSelectedStyle(Style s) {
        mv.setUnSelectedStyle(s);
    }

    /**
     * Gets the selected style of the month view component within the calendar
     * 
     * @return the style of the month view
     */
    public Style getMonthViewSelectedStyle() {
        return mv.getSelectedStyle();
    }
    
    /**
     * Gets the un selected style of the month view component within the calendar
     * 
     * @return the style of the month view
     */
    public Style getMonthViewUnSelectedStyle() {
        return mv.getUnselectedStyle();
    }

    /**
     * Fires when a change is made to the month view of this component
     * 
     * @param l listener to add
     */
    public void addActionListener(ActionListener l){
        mv.addActionListener(l);
    }
    
    /**
     * Fires when a change is made to the month view of this component
     * 
     * @param l listener to remove
     */
    public void removeActionListener(ActionListener l){
        mv.removeActionListener(l);
    }

    /**
     * Allows tracking selection changes in the calendar in real time
     * 
     * @param l listener to add
     */
    public void addDataChangeListener(DataChangedListener l) {
        mv.addDataChangeListener(l);
    }

    /**
     * Allows tracking selection changes in the calendar in real time
     * 
     * @param l listener to remove
     */
    public void removeDataChangeListener(DataChangedListener l) {
        mv.removeDataChangeListener(l);
    }
}
